<template>
	<view>
		<view class="goods-list">
			<view class="goods-item">
				<radio :checked="item.goods_state" color="#f00" @click="changeStatus(item.goods_id)" />
				<view class="">
					<image :src="item.goods_small_logo" mode=""></image>
				</view>
				<view class="goods-info">
					<view class="name">{{ item.goods_name }}</view>
					<view class="price-number">
						<view class="price">￥ {{ item.goods_price }}</view>
						<uni-number-box v-model="item.goods_count" @change="changeNumber" @blur="blur">
						</uni-number-box>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		name:"goods-item",
		data() {
			return {
				num: 0
			}
		},
		props: {
			item: {
				type:Object,
				required: true
			}
		},
		methods: {
			// 修改单个商品选择状态
			changeStatus(id) {
				this.$store.commit('cart/selectStatus', id)
			},
			// 修改单个商品数量
			changeNumber(e) {
				this.$store.commit('cart/goodsNumbeer', { id: this.item.goods_id, number: e })
			},
			blur(e) {
				let value =	parseInt(e.detail.value)
				// 如果不能转换为数字转换为NaN
				if (value && value > 0) {
					// 可以转换为数字并且大于0
						this.$store.commit('cart/goodsNumbeer', { id: this.item.goods_id, number: value })	
				} else {
					// 非数字或者转换为0，或者转换小于0
					this.$store.commit('cart/goodsNumbeer', { id: this.item.goods_id, number: 0 })
				}
			}
		}
	}
</script>

<style scoped lang="scss">
.goods-list {
	.goods-item {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 14rpx;
		image {
			width: 180rpx;
			height: 180rpx;
		}
		.goods-info {
			padding: 20rpx;
			font-size: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.name {
				font-size: 24rpx;
			}
			.price-number {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;
				.price {
					color: #f00;
				}
			}
		}
	}
}
</style>
